package com.liang.interview.server.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.base.Preconditions;
import com.liang.interview.api.req.InterviewReq;
import com.liang.interview.api.req.InterviewSubmitReq;
import com.liang.interview.api.req.StartReq;
import com.liang.interview.api.vo.InterviewQuestionVO;
import com.liang.interview.api.vo.InterviewResultVO;
import com.liang.interview.api.vo.InterviewVO;
import com.liang.interview.server.entity.po.InterviewHistory;
import com.liang.interview.server.entity.po.SubjectLabel;
import com.liang.interview.server.mapper.InterviewMapper;
import com.liang.interview.server.mapper.SubjectMapper;
import com.liang.interview.server.service.InterviewEngine;
import com.liang.interview.server.service.InterviewService;
import com.liang.interview.server.util.PDFUtil;
import com.liang.interview.server.util.keyword.KeyWordUtil;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class InterviewServiceImpl extends ServiceImpl<InterviewMapper, InterviewHistory> implements InterviewService, ApplicationContextAware {

    private static final Map<String, InterviewEngine> engineMap = new HashMap<>();

    private final SubjectMapper subjectMapper;
    public InterviewServiceImpl(SubjectMapper subjectMapper) {
        this.subjectMapper = subjectMapper;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        Map<String, InterviewEngine> engines = applicationContext.getBeansOfType(InterviewEngine.class);
        engines.forEach((k, v) -> engineMap.put(v.engineType().name(), v));
    }

    @Override
    public InterviewVO analyse(InterviewReq req) {
        List<String> keywords = buildKeywords(req.getUrl());
        InterviewEngine engine = engineMap.get(req.getEngine());
        Preconditions.checkNotNull(engine, "未找到对应的面试引擎");
        return engine.analyse(keywords);
    }
    
    private List<String> buildKeywords(String url) {
        String pdfText = PDFUtil.getPdfText(url);
        if (!KeyWordUtil.isInit()) {
            List<String> list = subjectMapper.listAllLabel()
                    .stream()
                    .map(SubjectLabel::getLabelName)
                    .collect(Collectors.toList());
            KeyWordUtil.addWord(list);
        }
        return KeyWordUtil.buildKeyWordsLists(pdfText);
    }

    @Override
    public InterviewQuestionVO start(StartReq req) {
        InterviewEngine engine = engineMap.get(req.getEngine());
        Preconditions.checkNotNull(engine, "未找到对应的面试引擎");
        return engine.start(req);
    }

    @Override
    public InterviewResultVO submit(InterviewSubmitReq req) {
        InterviewEngine engine = engineMap.get(req.getEngine());
        Preconditions.checkNotNull(engine, "未找到对应的面试引擎");
        return engine.submit(req);
    }
}
